package com.iespring.service.issue;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iespring.bean.admin.issue.bo.IssueCreateBO;
import com.iespring.bean.admin.issue.pojo.Issue;
import com.iespring.bean.admin.issue.pojo.IssueExample;
import com.iespring.bean.admin.issue.vo.IssueListVo;
import com.iespring.bean.admin.issue.vo.IssueNoDeletedVO;
import com.iespring.bean.common.BasePageInfo;
import com.iespring.mapper.admin.PromotionIssueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: 浩澄
 * @Date: 2022/12/29-10:52
 * @Mission: #
 */
@Service
public class IssueServiceImpl implements IssueService{

    @Autowired
    PromotionIssueMapper issueMapper;
    @Override
    public IssueListVo list(BasePageInfo info, String question) {

        PageHelper.startPage(info.getPage(),info.getLimit());

        IssueExample example = new IssueExample();

        IssueExample.Criteria criteria = example.createCriteria();

        if (question != null && !"".equals(question)){
            // 模糊查询要加上 "%""%"；同时做逻辑删除，判断 deleted 字段
            criteria.andQuestionLike("%"+question+"%");
        }
        criteria.andDeletedEqualTo(false);

        example.setOrderByClause(info.getSort()+" "+info.getOrder());
        List<Issue> issueList = issueMapper.selectByExample(example);

        PageInfo pageInfo = new PageInfo(issueList);

        IssueListVo issueListVo = new IssueListVo();

        issueListVo.setPage(pageInfo.getPageNum());
        issueListVo.setLimit(pageInfo.getPageSize());
        issueListVo.setPages(pageInfo.getPages());
        issueListVo.setList(issueList);
        issueListVo.setTotal(pageInfo.getTotal());

        return issueListVo;
    }

    @Override
    public IssueNoDeletedVO create(IssueCreateBO issueCreateBO) {
        Issue issue = new Issue();

        issue.setAddTime(new Date());
        issue.setUpdateTime(new Date());
        issue.setQuestion(issueCreateBO.getQuestion());
        issue.setAnswer(issueCreateBO.getAnswer());
        issue.setDeleted(false);

        int row = issueMapper.insertSelective(issue);

        if (row == 1){

            IssueNoDeletedVO issueNoDeletedVO = new IssueNoDeletedVO();

            issueNoDeletedVO.setAddTime(issue.getAddTime());
            issueNoDeletedVO.setUpdateTime(issue.getUpdateTime());
            issueNoDeletedVO.setAnswer(issueCreateBO.getAnswer());
            issueNoDeletedVO.setQuestion(issueCreateBO.getQuestion());
            issueNoDeletedVO.setId(issue.getId());
            return issueNoDeletedVO;
        }else {
            return null;
        }
    }

    @Override
    public Issue update(Issue issue) {

        Issue issueUpdate = new Issue();

        issueUpdate.setId(issue.getId());
        issueUpdate.setQuestion(issue.getQuestion());
        issueUpdate.setAnswer(issue.getAnswer());
        issueUpdate.setAddTime(issue.getAddTime());
        issueUpdate.setUpdateTime(new Date());
        issueUpdate.setDeleted(issue.getDeleted());

        int i = issueMapper.updateByPrimaryKeySelective(issue);

        return issueUpdate;
    }

    @Override
    public int delete(IssueNoDeletedVO issueNoDeletedVO) {

        Issue issue = new Issue();

        issue.setId(issueNoDeletedVO.getId());
        issue.setQuestion(issueNoDeletedVO.getQuestion());
        issue.setAnswer(issueNoDeletedVO.getAnswer());
        issue.setAddTime(issueNoDeletedVO.getAddTime());
        issue.setUpdateTime(new Date());
        issue.setDeleted(true);

        int row = issueMapper.updateByPrimaryKeySelective(issue);

        if (row == 1){
            return 200;
        }else {
            return 621;
        }
    }
}
